package com.chia.multienty.workflow.delegate;

import com.chia.multienty.core.domain.bo.workflow.WorkflowProcessNotifyBO;
import com.chia.multienty.core.domain.enums.FlowableVariables;
import com.chia.multienty.core.dubbo.service.DubboMultientyService;
import com.chia.multienty.core.rabbitmq.MultientyRabbitmqConstants;
import com.chia.multienty.core.util.SpringUtil;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component("rabbitmqServiceTask")
public class RabbitmqServiceTask implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {

        Map<String, Object> variables = execution.getVariables();
        WorkflowProcessNotifyBO notify = new WorkflowProcessNotifyBO();
        notify.setParentId(execution.getParentId())
                .setProcessDefinitionId(execution.getProcessDefinitionId())
                .setProcessInstanceId(execution.getProcessInstanceId())
                .setRootProcessInstanceId(execution.getRootProcessInstanceId())
                .setVariables(variables)
                ;

        DubboMultientyService dubboMultientyService = SpringUtil.getBean(
                "dubboMultientyService", DubboMultientyService.class);

        dubboMultientyService.sendRabbitImmediateMsg(notify,
                variables.containsKey(FlowableVariables.MQ_ROUTING_KEY.name())
                        ? variables.get(FlowableVariables.MQ_ROUTING_KEY.name()).toString()
                        : MultientyRabbitmqConstants.WORKFLOW_NOTIFY_ROUTING_KEY,
                execution.getVariable(FlowableVariables.MQ_BO_TYPE.name()).toString(),
                execution.getVariable(FlowableVariables.MQ_META_ID.name(), Long.class),
                true
                );
    }
}
